package com.ukao.pad.ui.setting;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.tencent.bugly.beta.Beta;
import com.ukao.pad.R;
import com.ukao.pad.base.BaseFragment;
import com.ukao.pad.utils.CheckUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

import static com.ukao.pad.R.id.print_receipt_btn;
import static com.ukao.pad.R.id.upgrade_btn;
import static com.ukao.pad.R.id.wifi_btn;


public class SettingFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(wifi_btn)
    RadioButton wifiBtn;
    @BindView(R.id.link_btn)
    RadioButton linkBtn;
    @BindView(print_receipt_btn)
    RadioButton printBtn;
    @BindView(R.id.station_btn)
    RadioButton stationBtn;
    @BindView(R.id.warning_tone_btn)
    RadioButton warningToneBtn;
    @BindView(R.id.rfid_btn)
    RadioButton rfidBtn;
    @BindView(R.id.about_btn)
    RadioButton aboutBtn;

    @BindView(upgrade_btn)
    RadioButton upgradeBtn;
    @BindView(R.id.factory_info_btn)
    RadioButton factoryInfoBtn;
    Unbinder unbinder;
    private SupportFragment[] mFragments=new SupportFragment[8] ;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private int defSelecePos;


    public static SettingFragment newInstance(int defSelecePos, String param2) {
        SettingFragment fragment = new SettingFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, defSelecePos);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            defSelecePos= getArguments().getInt(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }
    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!CheckUtils.isMIUIDevices())
            Beta.checkUpgrade(false,false);
    }


    @Override
    protected void initView() {
        SupportFragment firstFragment = findChildFragment(FactoryInformationFragment.class);
        if (firstFragment == null) {
            Log.d("进来了1", "initView: ");
            mFragments[0] =  FactoryInformationFragment.newInstance("", "");
            mFragments[1] =  WifiListFragment.newInstance("", "");
          /*  mFragments[1] = WiredNetworkFragment.newInstance("", "");*/
            mFragments[2] = PrintFragment.newInstance("","");
//            mFragments[3] = StationFragment.newInstance("", "");
//            mFragments[4] = ParameterFragment.newInstance("", "");
            mFragments[3] = StationFragment.newInstance(false, "");
            mFragments[4] = AboutLocalFragment.newInstance("", "");
            mFragments[5] = VersionFragment.newInstance("", "");
            mFragments[6] = ParameterFragment.newInstance("", "");
            loadMultipleRootFragment(R.id.set_layout, defSelecePos,
                    mFragments[0],
                    mFragments[1]
                    ,mFragments[2],mFragments[3],mFragments[4],mFragments[5],  mFragments[6]);
        } else {
            mFragments[0] = firstFragment;
/*            mFragments[1] = findChildFragment(WiredNetworkFragment.class);*/
            mFragments[1] = findChildFragment(WifiListFragment.class);
            mFragments[2] = findChildFragment(PrintFragment.class);
            mFragments[3] = findChildFragment(StationFragment.class);
//            mFragments[4] = findChildFragment(ParameterFragment.class);

            mFragments[4] = findChildFragment(AboutLocalFragment.class);
            mFragments[5] = findChildFragment(VersionFragment.class);
            mFragments[6] = findChildFragment(ParameterFragment.class);
        }
        switch (defSelecePos) {
            case   0:
                factoryInfoBtn.setChecked(true);
                break;
            case 1:
                wifiBtn.setChecked(true);
                break;
            case 2:

                printBtn.setChecked(true);
                break;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnCheckedChanged({wifi_btn, R.id.link_btn, print_receipt_btn, R.id.station_btn, R.id.warning_tone_btn,
            R.id.rfid_btn, R.id.about_btn,R.id.upgrade_btn, R.id.factory_info_btn,R.id.setting_parameter})
    public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
        if (checked){
            switch (buttonView.getId()) {
                case   R.id.factory_info_btn:
                    showHideFragment(mFragments[0]);
                break;
                case wifi_btn:
                    showHideFragment(mFragments[1]);
                    break;
                case R.id.link_btn:
                   // showHideFragment(mFragments[1]);
                    break;
                case print_receipt_btn:
                    showHideFragment(mFragments[2]);
                    break;
                case R.id.station_btn:
                    showHideFragment(mFragments[3]);
                    break;
                case R.id.warning_tone_btn:
                 //   showHideFragment(mFragments[4]);
                    break;
                case R.id.rfid_btn:
                  //  showHideFragment(mFragments[5]);
                    break;
                case R.id.about_btn:
                    showHideFragment(mFragments[4]);
                    break;
                case R.id.upgrade_btn:
                    Beta.checkUpgrade(false, false);
                    showHideFragment(mFragments[5]);
                    break;

                case R.id.setting_parameter:
                    showHideFragment(mFragments[6]);
                    break;
            }
        }
        }
}
